//
//  HQShopFiltrateView.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/8.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQShopFiltrateView.h"

@implementation HQShopFiltrateView

-(void)selctDict:(NSDictionary *)dict1 withDict2:(NSDictionary *)dict2 withDict3:(NSDictionary *)dict3 withDict4:(NSDictionary *)dict4  withDict5:(NSDictionary *)dict5{
    _dict1 = dict1;
    _dict2 = dict2;
    _dict3 = dict3;
    _dict4 = dict4;
    _dict5 = dict5;
    NSLog(@"%@%@%@%@%@",_dict1,_dict2,_dict3,_dict4,_dict5);
    [self.collectionView reloadData];
}

+ (instancetype)loadShopFiltrateView{
    return [[[NSBundle mainBundle] loadNibNamed:@"HQShopFiltrateView" owner:nil options:nil] lastObject];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 1, 0);
    _layout.minimumInteritemSpacing = 1;
    _layout.minimumLineSpacing = 1;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HQFiltrateViewCell" bundle:nil] forCellWithReuseIdentifier:@"HQFiltrateViewCell"];
    
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderID"];
}

-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataSource[section][@"itemArray"] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HQFiltrateViewCell *cell = [HQFiltrateViewCell cellWithCollectionView:collectionView withIndexPath:indexPath];
    NSDictionary *dict = _dataSource[indexPath.section][@"itemArray"][indexPath.row];
    if(indexPath.section == 0){
        if([_dict1 isEqual:dict]){
            cell.backView.backgroundColor = [self colorHexValue:0x65AEE8];
        }else{
            cell.backView.backgroundColor = [self colorHexValue:0xf1f2f3];
        }
        cell.itemName.text = dict[@"shop_name"];
    }else if(indexPath.section == 1){
        if([_dict2 isEqual:dict]){
            cell.backView.backgroundColor = [self colorHexValue:0x65AEE8];
        }else{
            cell.backView.backgroundColor = [self colorHexValue:0xf1f2f3];
        }
        cell.itemName.text = dict[@"tse_name"];
    }else if(indexPath.section == 2){
        if([_dict3 isEqual:dict]){
            cell.backView.backgroundColor = [self colorHexValue:0x65AEE8];
        }else{
            cell.backView.backgroundColor = [self colorHexValue:0xf1f2f3];
        }
        cell.itemName.text = dict[@"tpe_name"];
    }else if(indexPath.section == 3){
        if([_dict4 isEqual:dict]){
            cell.backView.backgroundColor = [self colorHexValue:0x65AEE8];
        }else{
            cell.backView.backgroundColor = [self colorHexValue:0xf1f2f3];
        }
        cell.itemName.text = dict[@"name"];
    }else if(indexPath.section == 4){
        if([_dict5 isEqual:dict]){
            cell.backView.backgroundColor = [self colorHexValue:0x65AEE8];
        }else{
            cell.backView.backgroundColor = [self colorHexValue:0xf1f2f3];
        }
        cell.itemName.text = dict[@"name"];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = _dataSource[indexPath.section][@"itemArray"][indexPath.row];
    if(indexPath.section == 0){
        _dict1 = dict;
    }else if(indexPath.section == 1){
        _dict2 = dict;
    }else if(indexPath.section == 2){
        _dict3 = dict;
    }else if(indexPath.section == 3){
        _dict4 = dict;
    }else if(indexPath.section == 4){
        _dict5 = dict;
    }
    [_collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-52)/3, 40);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH-50, 40);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderID" forIndexPath:indexPath];
        l = [[UILabel alloc]init];
        l.backgroundColor = [UIColor whiteColor];
        l.text = @"";
        l.text = [NSString stringWithFormat:@"  %@",_dataSource[indexPath.section][@"itemName"]];
        l.frame = headerView.bounds;
        l.textColor = [self colorHexValue:0x001138];
        l.font = [UIFont boldSystemFontOfSize:18];
        [headerView addSubview:l];
        return headerView;
    }
    return nil;
}

- (IBAction)resetItem:(id)sender {
    _dict1 = nil;
    _dict2 = nil;
    _dict3 = nil;
    _dict4 = nil;
    _dict5 = nil;
    [_collectionView reloadData];
}

- (IBAction)contentItem:(id)sender {
    [self.delegate filter2Dict:_dict1 withDict2:_dict2 withDict3:_dict3 withDict4:_dict4 withDict5:_dict5];
}

@end
